package com.tongzhou.tongzhou.update.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.update.dao.IUpdateCodeDAO;
import com.tongzhou.tongzhou.update.model.SearchUpdateCodeCondition;
import com.tongzhou.tongzhou.update.model.UpdateCode;
import com.tongzhou.util.Util;

public class UpdateCodeDAO implements IUpdateCodeDAO{

	@Override
	public UpdateCode getUpdateCodetById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,codeText,appUrl,status,date_format(create_time,'%Y-%m-%d') as create_time ");
		sql.append(" from update_code ");
		sql.append(" where id='"+id+"'");
		UpdateCode updateCode=DBUtil.query(sql.toString(), new BeanHandler<UpdateCode>(UpdateCode.class));
		return updateCode;
	}

	@Override
	public int addUpdateCode(UpdateCode updateCode) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into update_code(codeText,appUrl,status,create_time) ");
		sql.append("values('"+Util.changeToObj(updateCode.getCodeText())+"',");
		sql.append("'"+Util.changeToObj(updateCode.getAppUrl())+"',");
		sql.append("'"+Util.changeToObj(updateCode.getStatus())+"',");
		sql.append("now())");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deleteUpdateCode(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from update_code where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public UpdateCode getUpdateCodeById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,codeText,appUrl,status,create_time ");
		sql.append(" from update_code ");
		sql.append(" where id='"+id+"'");
		UpdateCode updateCode=DBUtil.query(sql.toString(), new BeanHandler<UpdateCode>(UpdateCode.class));
		return updateCode;
	}

	@Override
	public List<UpdateCode> listUpdateCode(SearchUpdateCodeCondition condition)
			throws DBException {
		List<UpdateCode> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from update_code ");
		sql.append(" where 1=1 ");
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,codeText,appUrl,status,create_time ");
		sql.append(" from update_code ");
		sql.append(" where 1=1 ");
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<UpdateCode>(UpdateCode.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateUpdateCode(UpdateCode updateCode) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update update_code set ");
		sql.append("codeText='"+Util.changeToObj(updateCode.getCodeText())+"',");
		sql.append("appUrl='"+Util.changeToObj(updateCode.getAppUrl())+"',");
		sql.append("status='"+Util.changeToObj(updateCode.getStatus())+"' ");
		sql.append("where id='"+updateCode.getId()+"'");
		DBUtil.update(sql.toString());
	}

}
